<?php
/**
 * 
 * REST API caller
 * 
 * @version $Id$
 * @author deathemperor
 *
 */
class SGN_Rest
{
    public static function postData ($url, $param, $timeout)
    {
        $startTime = microtime(true);
        if ($timeout < 0) {
            throw new Exception('Invalid Timeout. Timeout must be integer in miliseconds.');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        $value = json_decode($result, TRUE);
        if (SGN_Application::isRegistered('profiler')) {
            $totalTime = (microtime(true) - $startTime) * 1000; // in miliseconds
            SGN_Application::get('profiler')->setProfilerData('REST_API', array("$url", $totalTime, $param));
        }
        if (! $error) {
            return $value;
        } else {
            $_logData['msg'] = 'Cannot get data ' . $error;
            $_logData['state'] = 'CANNOT GET DATA';
            SGN_Log::writeLog(__CLASS__, __FUNCTION__, $_logData);
            throw new SGN_Exception($_logData['msg']);
        }
    }
    /**
     * POST Notification Data
     * 
     * @param string $url
     * @param array $param
     * @param integer $timeout
     * 
     * @return string
     * 
     */
    public static function postNotif ($url, $param, $timeout)
    {
        $startTime = microtime(true);
        if ($timeout < 0) {
            throw new Exception('Invalid Timeout. Timeout must be integer in miliseconds.');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if (SGN_Application::isRegistered('profiler')) {
            $totalTime = (microtime(true) - $startTime) * 1000; // in miliseconds
            SGN_Application::get('profiler')->setProfilerData('REST_API', array("$url", $totalTime, $param));
        }
        if (! $error) {
            return $result;
        } else {
            $_logData['msg'] = 'Cannot get data. ' . $error;
            SGN_Log::writeLog(__CLASS__, __FUNCTION__, $_logData);
            throw new SGN_Exception($_logData['msg']);
        }
    }
    /**
     * 
     * Call REST APIs
     * 
     * @param string $url
     * @param array $param
     * @param integer $timeout in miliseconds
     * @throws Exception
     * @return mixed|string
     */
    public static function getData ($url, $param, $timeout)
    {
        $startTime = microtime(true);
        $timeout = (int)$timeout;
        if ($timeout < 0) {
            throw new Exception('Invalid Timeout. Timeout must be integer in miliseconds.');
        }
        $ch = curl_init();
        $i = 1;
        $len = sizeof($param);
        foreach ($param as $key => $value) {
            $url .= $key . '=' . $value;
            if ($i < $len)
                $url .= '&';
            $i ++;
        }
        ChromePhp::log($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, 155, $timeout); // CURLOPT_TIMEOUT_MS
        curl_setopt($ch, 156, $timeout); // CURLOPT_CONNECTTIMEOUT_MS
        $result = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        $value = json_decode($result, TRUE);
        if (SGN_Application::isRegistered('profiler')) {
            $totalTime = (microtime(true) - $startTime) * 1000; // in miliseconds
            SGN_Application::get('profiler')->setProfilerData('REST_API', array("$url", $totalTime, $param));
        }
        if (! $error) {
            return $value;
        } else {
            $_logData['msg'] = 'Cannot get data';
            $_logData['state'] = 'CANNOT GET DATA';
            return $_logData;
        }
    }
}